package com.group.eams.controller.system;

import com.group.eams.entity.Employee;
import com.group.eams.service.IEmployeeService;
import com.group.eams.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by zcy-fover on 2016/11/3.
 * 员工行为控制器
 */
@Controller
@RequestMapping("/system/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/query")
    public ModelAndView query(int select, String text){
        ModelAndView modelAndView = new ModelAndView();
        List<Employee> employeeList;
        if (select == 1){
            employeeList = employeeService.getEmployeeByName(text);
        } else {
            employeeList = employeeService.getEmployeeBySex(select);
        }
        modelAndView.addObject("employeeList", employeeList);
        modelAndView.setViewName("/system/employee/employeeList");
        return modelAndView;
    }

    @RequestMapping("/list")
    public ModelAndView employeeList() {
        ModelAndView modelAndView = new ModelAndView();
        List<Employee> employeeList = employeeService.getEmployeeList();
        modelAndView.addObject("employeeList", employeeList);
        modelAndView.setViewName("/system/employee/employeeList");
        return modelAndView;
    }

    @RequestMapping("/detail")
    public ModelAndView detail(int id) {
        ModelAndView modelAndView = new ModelAndView("/system/employee/detail");
        Employee employee = employeeService.findEmployeeById(id);
        modelAndView.addObject("employee", employee);
        return modelAndView;
    }

    @RequestMapping("/toEdit")
    public ModelAndView toEditEmployee(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        if (id == null) {
            modelAndView.setViewName("system/employee/addEmployee");
            return modelAndView;
        } else {
            Employee employee = employeeService.findEmployeeById(id);
            modelAndView.setViewName("system/employee/editEmployee");
            modelAndView.addObject("employee", employee);
            return modelAndView;
        }
    }

    @RequestMapping("/save")
    public ModelAndView save(@ModelAttribute Employee employee, HttpServletRequest request) {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        ModelAndView modelAndView = new ModelAndView();
        Result result = new Result();
        if (employee.getId() == null) {
            System.out.println("执行添加");
            System.out.println(employee.getName());
            employee.setEmployeeType(0);
            employee.setState(0);
            result = employeeService.addEmployee(employee);
        } else {
            System.out.println("执行修改");
            result = employeeService.updateEmployee(employee);
        }
        List<Employee> employeeList = employeeService.getEmployeeList();
        modelAndView.addObject("employeeList", employeeList);
        modelAndView.addObject("result", result);
        modelAndView.setViewName("/system/employee/employeeList");
        return modelAndView;
    }

    @RequestMapping("/delete")
    public ModelAndView delete(Integer id){
        Result result = new Result();
        ModelAndView modelAndView = new ModelAndView();
        result = employeeService.deleteEmployeeById(id);
        List<Employee> employeeList = employeeService.getEmployeeList();
        modelAndView.addObject("employeeList", employeeList);
        modelAndView.addObject("result", result);
        modelAndView.setViewName("/system/employee/employeeList");
        return modelAndView;
    }

}
